package km.TestCrypto;

import java.security.*;

import km.TestCtypto.*;
import android.app.Activity;
import android.os.Bundle;
import android.text.*;
import android.view.*;
import android.view.View.*;
import android.view.inputmethod.*;
import android.widget.*;

public class TestCrypto extends Activity {
    private EditText ed;
	private TextView tv;
	private Button btn;
String id="";
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ed = (EditText)findViewById(R.id.ed);
        tv = (TextView)findViewById(R.id.tv);
        btn = (Button)findViewById(R.id.btn);
        ed.setInputType(InputMethod.SHOW_EXPLICIT);
        btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				try{
					id=	SimpleCrypto.encrypt("id", ed.getText().toString());
					tv.setText("AES : "+id);
					tv.append("\n");
					tv.append("\n md5 : "+md5(ed.getText().toString()));
					tv.append("\n");
					tv.append("\n sha-256 : "+SHA256(ed.getText().toString()));
				}catch (Exception e) {
						e.printStackTrace();
				}
			}
		});
        
    }
    
    public String md5(String s) {   
        try {   
            // Create MD5 Hash   
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");   
            digest.update(s.getBytes());   
            byte messageDigest[] = digest.digest();   
               
            // Create Hex String   
            StringBuffer hexString = new StringBuffer();   
            for (int i=0; i<messageDigest.length; i++)   
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));  
//            hexString.append(String.format("%1$02x", messageDigest[i]));   
            return hexString.toString();   
               
        } catch (NoSuchAlgorithmException e) {   
            e.printStackTrace();   
        }   
        return "";   
    }  
    public String SHA256(String s) {   
        try {   
            // Create MD5 Hash   
            MessageDigest digest = java.security.MessageDigest.getInstance("SHA-256");   
            digest.update(s.getBytes());   
            byte messageDigest[] = digest.digest();   
               
            // Create Hex String   
            StringBuffer hexString = new StringBuffer();   
            for (int i=0; i<messageDigest.length; i++)   
                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));   
            return hexString.toString();   
               
        } catch (NoSuchAlgorithmException e) {   
            e.printStackTrace();   
        }   
        return "";   
    }
}